package com.ncbee.base.config.beetl;

import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: fengzirong
 * @Description:
 * @Date:Create in 14:26 2018/9/11
 */
@Configuration
public class CustomBeetlConfig {
    private static final Logger logger = LoggerFactory.getLogger(CustomBeetlConfig.class);
    @Bean(initMethod = "init",name ="beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        logger.info("------正在初始化beetlConfig-------");
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader();
        beetlGroupUtilConfiguration.setResourceLoader(classpathResourceLoader);
        beetlGroupUtilConfiguration.init();
        /*beetlGroupUtilConfiguration.getGroupTemplate().registerFunctionPackage("shiro", new ShiroExt());*/
        return beetlGroupUtilConfiguration;
    }
    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(
            @Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        logger.info("--------正在初始化beetlViewResolver-------");
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setPrefix("/WEB-INF/views/");
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        logger.info("--------加入自定义shiro标签初始化-------");
        beetlSpringViewResolver.setGroupTemplate(getGroupTemplate());
        return beetlSpringViewResolver;
    }
    @Bean
    public GroupTemplate getGroupTemplate(){

        GroupTemplate groupTemplate=new GroupTemplate();
        groupTemplate.registerFunctionPackage("shiro", new ShiroExt());
        return groupTemplate;
    }
}
